﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using freshflowersHTK.Helpers;
using freshflowersHTK.Models;

namespace freshflowersHTK.Controllers
{
    public class ProductController : Controller
    {
        private readonly FreshFlowerDbContext _db = new FreshFlowerDbContext();
        private bool? _isAuthenticated;
        private readonly FreshFlowerHelper _helper = new FreshFlowerHelper();
        //
        // GET: /Product/

        public ActionResult Index()
        {
            CheckForAuthentication();

            var products = _db.Products.Include(p => p.ProductStatus).Include(p => p.Category)
                .Include(p => p.Catalog).OrderByDescending(p => p.ProductID);
            return View(products.ToList());
        }

        protected void CheckForAuthentication()
        {
            return;
            if (IsAuthenticated) return;
            Response.Redirect("/Landing/Index", true);
        }

        protected bool IsAuthenticated
        {
            get
            {
                if (_isAuthenticated == null)
                {
                    if (Session[Constants.SessionIsAuthenticated] == null) return false;

                    _isAuthenticated = (bool)Session[Constants.SessionIsAuthenticated];
                }
                return _isAuthenticated.Value;
            }
        }

        //
        // GET: /Product/Details/5

        public ActionResult Details(int id = 0)
        {
            CheckForAuthentication();

            Products products = _db.Products.Find(id);
            if (products == null)
            {
                return HttpNotFound();
            }
            return View(products);
        }

        //
        // GET: /Product/Create

        public ActionResult Create()
        {
            CheckForAuthentication();
            ViewBag.ProductStatusId = new SelectList(_db.ProductStatus, "ProductStatusId", "ProductStatusLiteral");
            ViewBag.CategoryId = new SelectList(_db.Category, "CategoryId", "CategoryName");
            ViewBag.CatalogId = new SelectList(_db.Catalog, "CatalogId", "CatalogName");
            return View();
        }

        //
        // POST: /Product/Create

        [HttpPost]
        public ActionResult Create(Products products)
        {
            if (ModelState.IsValid)
            {
                _db.Products.Add(products);
                _db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.ProductStatusId = new SelectList(_db.ProductStatus, "ProductStatusId", "ProductStatusLiteral", products.ProductStatusId);
            ViewBag.CategoryId = new SelectList(_db.Category, "CategoryId", "CategoryName", products.CategoryId);
            ViewBag.CatalogId = new SelectList(_db.Catalog, "CatalogId", "CatalogName", products.CatalogId);
            return View(products);
        }

        //
        // GET: /Product/Edit/5

        public ActionResult Edit(int id = 0)
        {
            CheckForAuthentication();

            Products products = _db.Products.Find(id);
            if (products == null)
            {
                return HttpNotFound();
            }
            ViewBag.ProductStatusId = new SelectList(_db.ProductStatus, "ProductStatusId", "ProductStatusLiteral", products.ProductStatusId);
            ViewBag.CategoryId = new SelectList(_db.Category, "CategoryId", "CategoryName", products.CategoryId);
            ViewBag.CatalogId = new SelectList(_db.Catalog, "CatalogId", "CatalogName", products.CatalogId);
            return View(products);
        }

        //
        // POST: /Product/Edit/5

        [HttpPost]
        public ActionResult Edit(Products products)
        {
            if (ModelState.IsValid)
            {
                _db.Entry(products).State = EntityState.Modified;
                _db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.ProductStatusId = new SelectList(_db.ProductStatus, "ProductStatusId", "ProductStatusLiteral", products.ProductStatusId);
            ViewBag.CategoryId = new SelectList(_db.Category, "CategoryId", "CategoryName", products.CategoryId);
            ViewBag.CatalogId = new SelectList(_db.Catalog, "CatalogId", "CatalogName", products.CatalogId);
            return View(products);
        }

        //
        // GET: /Product/Delete/5

        public ActionResult Delete(int id = 0)
        {
            CheckForAuthentication();

            Products products = _db.Products.Find(id);
            if (products == null)
            {
                return HttpNotFound();
            }
            return View(products);
        }

        //
        // POST: /Product/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Products products = _db.Products.Find(id);
            _db.Products.Remove(products);
            _db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            _db.Dispose();
            base.Dispose(disposing);
        }
    }
}